package org.example.util;

import org.springframework.core.io.ClassPathResource;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class JsonUtil {
    /**
     * 读取JSON文件转换为字符串
     * @param inputStream
     * @return
     */
    public static String readJsonFile(InputStream inputStream) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            int ch;
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            reader.close();
            return sb.toString();
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        try{
            ClassPathResource resource = new ClassPathResource("entityQuery.json");
            InputStream inputStream = null;
            try {
                inputStream = resource.getInputStream();
            } catch (Exception e) {
                e.printStackTrace();
            }
            String jsonString = JsonUtil.readJsonFile(inputStream);
            System.out.println(jsonString);
        } catch (Exception e){
            e.printStackTrace();
        }

    }
}
